/**
 * @file VerPruebasPendientes.java
 * @brief Interfaz que muestra las pruebas pendientes
 *
 * Copyright (C)  - May [2011]
 *
 * @author Carlos Castilla Ruiz
 * @version 01.00
 *
 */

package interfaces;

import interfaces.farmacia.*;


import Controlador.*;
import GestorCitas.*;
import GestorPruebas.*;
import GestorUsuarios.IDException;
import GestorUsuarios.Paciente;
import GestorUsuarios.Usuario;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.awt.event.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;


//QUEDA POR HACER LA GRAN MAYORIA!!!!!!
public class VerPruebasPendientes extends javax.swing.JPanel{

    private TableModel _modelo;
    private ClinicaView _app;
    private Prueba[]  _prueba ;
    Interfaces _anterior,_actual;

    /** Creates new customizer NewCustomizer */
    public VerPruebasPendientes(ClinicaView a) {
        _app = a;
        initComponents();
    }

    public void rellenaDatos(Interfaces ant,Interfaces act) throws GestorUsuarios.IDException, SQLException, IOException, ClassNotFoundException, SQLException, NoSuchAlgorithmException, ParseException {
       DateFormat fecha_formato = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        _anterior = ant;
        _actual= act;

        // Limpiamos la tabla de pruebas
        Interfaz.limpiarTabla(panel_pruebas_pendientes);

        // Obtenemos todas las pruebas pendientes del tecnico de pruebas actual
        _prueba = Controlador.verPruebasPendientes(Controlador.getUsuarioActivo().getIdUsuario());
       

        if(_prueba == null) {
            _app.abrirMensajeError("No se encontró ninguna prueba pediente.", _anterior);
            return;
        }


        // Rellenamos la tabla
        Object[][] data =  new Object[_prueba.length][];
        for(int i=0;i<_prueba.length;i++) {
           Paciente p = (Paciente) Paciente.get(_prueba[i].getIdPaciente());
           data[i] = new Object[4];
           data[i][0] = fecha_formato.format(_prueba[i].getFecha());
           data[i][1] = p.getNombre()+" "+p.getApellidos();
           data[i][2] = _prueba[i].getTipo();
           data[i][3] = _prueba[i].getEstadoPrueba();
        }

        String[] columnNames = {"Hora de la prueba","Nombre del paciente","Tipo de prueba","Estado de la prueba"};
        _modelo = new DefaultTableModel(data, columnNames);
        panel_pruebas_pendientes.setModel(_modelo);
        panel_pruebas_pendientes.setRowHeight(32);

    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        panel_pruebas_pendientes = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setMaximumSize(new java.awt.Dimension(780, 470));
        setMinimumSize(new java.awt.Dimension(780, 470));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pruebas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setMaximumSize(new java.awt.Dimension(780, 470));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        panel_pruebas_pendientes.setFont(Interfaz.setFontSize());
        panel_pruebas_pendientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Hora de la prueba", "Nombre del paciente", "Tipo de prueba"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        panel_pruebas_pendientes.setName("panel_pruebas_pendientes"); // NOI18N
        jScrollPane1.setViewportView(panel_pruebas_pendientes);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 777, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(VerPruebasPendientes.class, this);
        jButton2.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(VerPruebasPendientes.class);
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setLabel(resourceMap.getString("jButton3.label")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("asignarResultado")); // NOI18N
        jButton3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText("Asignar resultado");
        jButton3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton3.setName("jButton3"); // NOI18N

        jButton4.setAction(actionMap.get("cancelar")); // NOI18N
        jButton4.setIcon(resourceMap.getIcon("jButton4.icon")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        jComboBox1.setFont(Interfaz.setFontSize());
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pendiente", "No llevada a cabo" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(Interfaz.setBoldFontSize());
        jLabel1.setText("Cambiar estado de la prueba");
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 66, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 76, Short.MAX_VALUE)
        );

        jPanel3.setName("jPanel3"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 65, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 76, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(46, 46, 46)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jButton3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 47, Short.MAX_VALUE)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(19, 19, 19))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(layout.createSequentialGroup()
                            .add(jLabel1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        int fila = panel_pruebas_pendientes.getSelectedRow();
        String exito=null;

        if(fila >= 0){
            Prueba pr=_prueba[fila];
            EstadoPrueba e=pr.getEstadoPrueba();

            int s = jComboBox1.getSelectedIndex();
            try{
                if(s==1 && e!=e.No_realizada){//2==No realizada
                    //pr.setEstadoPrueba(e.No_realizada);
                    //asignaEstado(pr);
                    exito=Controlador.cambiarEstadoPrueba(pr.getIdPrueba(),e.No_realizada);
                }
            }
            catch(java.security.NoSuchAlgorithmException nsae){
            }
            catch(GestorUsuarios.IDException ide){
            }
            catch(java.lang.ClassNotFoundException cnfe){
            }
            catch(java.io.IOException ioe){
            }
            catch(java.text.ParseException pe){
            }

            if(exito==null) {
                _app.abrirMensajeExito("El estado ha sido modificado correctamente", _anterior);
            } else {
                _app.abrirMensajeError(exito,_anterior);
            }
        }else{
            _app.limpiaErrores();
            _app.addError("No ha seleccionado ninguna prueba");
            _app.muestraListaErrores();
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed
   /** Por si es necesaria
    public void asignaEstado(Prueba p) throws java.security.NoSuchAlgorithmException,GestorUsuarios.IDException,java.lang.ClassNotFoundException,java.io.IOException,java.sql.SQLException{
        String exito=null;
        exito = Controlador.asignarResultados(p);
        if(exito==null) {
            _app.abrirMensajeExito("El estado ha sido modificado correctamente", _anterior);
        } else {
            _app.abrirMensajeError(exito,_anterior);
        }
    }*/

    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        _app.volver(_anterior);
    }

      @Action
    public void asignarResultado()throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        int fila = panel_pruebas_pendientes.getSelectedRow();
        
        if(fila >= 0){
            Prueba pr=_prueba[fila];

            switch (pr.getTipo()) {
                case HematologiaGeneral:
                     _app.abrirAsignarResultadoPruebaHematologia(pr, _actual);
                    break;
                case BioquimicaGeneral:
                    _app.abrirAsignarResultadoPruebaBioquimicaGeneral(pr,_actual);
                    break;
                case MarcadoresTumorales:
                    _app.abrirAsignarResultadoPruebaMarcadoresTumorales(pr,_actual);
                    break;
                case Hormonas:
                    _app.abrirAsignarResultadoPruebaHormonas(pr,_actual);
                    break;
                case Urianalisis:
                    _app.abrirAsignarResultadoPruebaUrianalisis(pr,_actual);
                    break;
                case Hemostasia:
                    _app.abrirAsignarResultadoPruebaHemostasia(pr,_actual);
                    break;
                case Radiografia:
                    _app.abrirAsignarResultadoPruebaRadiografia(pr, _actual);
                    break;
                default:
                    break;
            }
        }else{
            _app.limpiaErrores();
            _app.addError("No ha seleccionado ninguna prueba");
            _app.muestraListaErrores();
        }
    }

    @Action
    public void cancelar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException{
        _app.volver(_anterior);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable panel_pruebas_pendientes;
    // End of variables declaration//GEN-END:variables


}
